package com.qingneng.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mvel2.MVEL;

import com.opensymphony.xwork2.Action;
import com.qingneng.service.sdk.IWorkBudgetService;
import com.qingneng.vo.BudgetItem;
import com.qingneng.vo.BudgetWorkDoInfo;
import com.qingneng.vo.BudgetWorkQuantities;
import com.tbfs.common.BaseAction;
import com.tbfs.util.UtilTool;

public class LoadBudgetItemAction extends BaseAction{
	
	private static final Logger logger = Logger.getLogger(LoadBudgetItemAction.class);
	
	private int project_workinfo_id;
	
	public int getProject_workinfo_id() {
		return project_workinfo_id;
	}
	public void setProject_workinfo_id(int project_workinfo_id) {
		this.project_workinfo_id = project_workinfo_id;
	}

	private List rows = null;
	
	private Integer total;
	
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	public Integer getTotal() {
		return total;
	}
	public void setTotal(Integer total) {
		this.total = total;
	}

	public IWorkBudgetService workBudgetService;
	
	public LoadBudgetItemAction(){
		
	}
	public LoadBudgetItemAction(IWorkBudgetService workBudgetService){
		this.workBudgetService = workBudgetService;
	}
	
	public String execute(){
		// 判断是否已经保存过预算表
		logger.info("工程id-project_workinfo_id:"+project_workinfo_id);
		
		BudgetWorkDoInfo bean =  new BudgetWorkDoInfo();
		bean.setProject_workinfo_id(project_workinfo_id);
		bean.setParent_item_id(0);
		logger.info("+++开始获取预算表结果+++");
		List workDolist = workBudgetService.getWorkDoInfoByWorkInfoIdAndItemId(bean);
		if(workDolist!=null && workDolist.size()>0){
			logger.info("LoadBudgetItemAction :工程id-project_workinfo_id:"+project_workinfo_id+"：有预算表结果");
			rows = workDolist;
			total = new Integer(1000);
		}else{
			logger.info("LoadBudgetItemAction:工程id-project_workinfo_id:"+project_workinfo_id+"：无预算表结果，获取预算项模板");
			BudgetItem budgetItem = new BudgetItem();
			budgetItem.setParent_item_id(0);
			rows = workBudgetService.getBudgetItem(budgetItem);
			//判断是否已经计算了渣土运输，施工人员消耗
			BudgetWorkQuantities budgetWorkQuantities = new BudgetWorkQuantities ();
			budgetWorkQuantities.setProject_workinfo_id(project_workinfo_id);
			List list = workBudgetService.getWorkQuantities(budgetWorkQuantities);
			if(list!=null && list.size()>0){
				BudgetWorkQuantities wq  = (BudgetWorkQuantities)list.get(0);
				String ec =  wq.getEffectiveCapacity();
				String sl =  wq.getSludgeCapacity();
				for(int i=0;i<rows.size();i++ ){
					BudgetItem item =(BudgetItem) rows.get(i);
					//渣土运输
					if("hfc_ztys".equals(item.getBudget_item_name())){
						String formual = item.getItem_formula();
						double unit_price = UtilTool.string2doubleFormat(item.getItem_unit_price());
						Map<String, Object> vars = new HashMap<String, Object>();
						//使用污泥容积
				        vars.put("$arg$", new Double(sl));   
				        Double budget_do_quantities = (Double )MVEL.eval(formual, vars); 
				        Double totalAmount = budget_do_quantities*unit_price;
				        logger.info("result:"+budget_do_quantities);
				        item.setBudget_do_quantities(String.valueOf(UtilTool.doubleFormat(budget_do_quantities)));
				        item.setTotalAmount(UtilTool.doubleFormat(totalAmount));
								
					}
					// 施工人员消耗
					if("sgry_xh".equals(item.getBudget_item_name())){
						String formual = item.getItem_formula();
						double unit_price = UtilTool.string2doubleFormat(item.getItem_unit_price());
						Map<String, Object> vars = new HashMap<String, Object>(); 
						//使用有效容积
				        vars.put("$arg$", new Double(ec));   
				        Double budget_do_quantities = (Double )MVEL.eval(formual, vars); 
				        Double totalAmount = budget_do_quantities*unit_price;
				        logger.info("result:"+budget_do_quantities);
				        item.setBudget_do_quantities(String.valueOf(UtilTool.doubleFormat(budget_do_quantities)));
				        item.setTotalAmount(UtilTool.doubleFormat(totalAmount));
								
					}
				}
			
			}
			
			
			total = new Integer(1000);
		}
	
		
		return Action.SUCCESS;
	}

}
